import json
import os
from config import BASE_PATH

"""
    # File       : file_tool.py
    # Time       ：2023/10/12 13:51
    # Author     ：曾伟雄
    # version    ：V1.0.0
    # Description：文件工具类
"""


class FileTool():

    # 读取json文件
    @classmethod
    def read_file(cls, file_name):
        filePath = BASE_PATH + os.sep + "data" + os.sep + file_name
        with open(filePath, mode="r", encoding="utf-8") as f:
            json_data = json.load(f)
            # print(json_data) # => [[],[],[],[]] 或者 ["","",""]
            list_data = []
            # 获取列表
            # 遍历列表，数据转元组
            for data in json_data:
                list_data.append([data])
                # list_data.append(data)
            return list_data

    # 写入json文件
    @classmethod
    def write_file(cls, origin_file_name, target_file_name):
        # 读取data.txt
        filePath = BASE_PATH + os.sep + "data" + os.sep + origin_file_name
        target_filePath = BASE_PATH + os.sep + "data" + os.sep + target_file_name
        all_data = []
        with open(filePath, mode="r", encoding="utf-8") as f:
            # 推导式去除每条数据尾部的换行符
            all_data = [line.strip() for line in f.readlines()]
        # 写入json
        with open(target_filePath, mode="w", encoding="utf-8") as f:
            json.dump(all_data, f, ensure_ascii=False, indent=2)

    @classmethod
    def write_file_from_csv(cls, origin_file_name, target_file_name, is_no_first):
        """
          通过csv文件生成json
          :param origin_file_name: 原始文件名
          :param target_file_name: 目标文件名
          :param is_no_first: 读取是否跳过第一行 True 则跳过
          :return:
        """
        filePath = BASE_PATH + os.sep + "data" + os.sep + origin_file_name
        target_filePath = BASE_PATH + os.sep + "data" + os.sep + target_file_name
        all_data = []
        with open(filePath, mode="r", encoding="utf-8") as f:
            if is_no_first:
                # 跳过第一行
                f.readline()
            # 遍历每一行
            for data in f:
                text = data.strip('"').strip()
                if text[-1] == '"':
                    text = text[:-1]
                # print(text)
                if text.startswith("http"):
                    all_data.append(text)
        # 写入json
        # 避免重复，列表去重
        seen = set()
        unique_list = [x for x in all_data if not (x in seen or seen.add(x))]
        with open(target_filePath, mode="w", encoding="utf-8") as f:
            json.dump(unique_list, f, ensure_ascii=False, indent=2)

    @classmethod
    def write_file_from_data(cls, data, target_file_name):
        """
        通过数据，直接生成json文件
        :param data: 数据
        :param target_file_name: 目标文件名
        :return:
        """
        with open(BASE_PATH + os.sep + "data" + os.sep + target_file_name, "w") as f:
            json.dump(data, f, indent=4)  # indent=4 用于格式化输出，使其更加可读


if __name__ == "__main__":
    # 生成参数化需要的json文件
    # FileTool().write_file("data.txt","data.json")
    # 读取参数化数据
    # data = FileTool().read_file("data.json")
    # print(data)
    FileTool().write_file_from_csv("storage_download.csv", "data.json", True)
